package edu.upenn.cis542;
import java.util.ArrayList;
import java.util.Hashtable;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class PaintBrushView extends View {

	public static ArrayList<Integer> xcord=new ArrayList<Integer>();
	public static ArrayList<Integer> ycord=new ArrayList<Integer>();
	public static ArrayList<Switcher> colorTracker=new ArrayList<Switcher>();
	public boolean start=true;;
	public static Switcher obj;
	public static int srcX,srcY,destX,destY;
	static int drawFlag=0;
	public Paint p=new Paint();
	public Path path;
	public boolean drawpoint=false;
	public int pointX;
	public int pointY;
	public int pointColor;
	public long downTime;
	public long upTime;
	public static Canvas canvas;
	
	public PaintBrushView(Context context) {
		super(context);
		
		
		init();
		// TODO Auto-generated constructor stub
	}
	
	public PaintBrushView(Context context, AttributeSet a) {
		super(context,a);
		init();
		// TODO Auto-generated constructor stub
	}
	
	protected ShapeDrawable square;
	protected void init()
	{
		square = new ShapeDrawable(new RectShape());
		square.getPaint().setColor(Color.RED);
		square.setBounds(80, 120, 80+60, 120+60);
	}
	
	protected void onDraw(Canvas canvas)
	{
		PaintBrushView.canvas = canvas;
		 if (PaintBrushActivity.clear == true) {
			canvas.drawColor(Color.BLACK);
			PaintBrushActivity.clear = false;
		}
		 
		
		 else {
				p.setColor(PaletteView.selectedColor);
				if (PaletteView.selectedThick == 0) {
					p.setStrokeWidth(2);
				} else if (PaletteView.selectedThick == 1) {
					p.setStrokeWidth(6);
				}
				try{
					int j=0;
					obj = colorTracker.get(j);
				for (int i = 1; i < xcord.size(); i++) {
					 if (xcord.get(i) < 0 || xcord.get(i - 1) < 0) 
					{
						p.setColor(Color.BLACK);
						canvas.drawLine(Math.abs(xcord.get(i - 1)),Math.abs(ycord.get(i - 1)), Math.abs(xcord.get(i)),Math.abs(ycord.get(i)), p);
					}
					else 
					{
						p.setColor(obj.getColor());
						System.out.println("THICK ==> " + obj.getThickness());
						if(obj.getThickness() == 1)
							p.setStrokeWidth(6);
						else 
							p.setStrokeWidth(2);
						
						canvas.drawLine(xcord.get(i - 1), ycord.get(i - 1),xcord.get(i), ycord.get(i), p);
					}
					if (obj.getEnd() > -1) 
					{
						if(i<=obj.getEnd())
						{
							;
						}
						else{
						j++;
						obj = colorTracker.get(j);
						
						}
					}
					else
					{
						j=0;
					}
					
					
				
	
				}//for ends
			}
		catch(Exception ae)
			{
				Context context = PaintBrushActivity.con;
				CharSequence text = "No Color has been Selected !";
				int duration = Toast.LENGTH_SHORT;
				PaintBrushView.xcord.clear();
		    	PaintBrushView.ycord.clear();
		    	PaintBrushView.colorTracker.clear();
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
			
		}
	}
	
	public boolean onTouchEvent(MotionEvent event) 
	{
		
		if(event.getAction() == MotionEvent.ACTION_UP)
    	{
			xcord.add((int)event.getX()*-1);
    			ycord.add((int)event.getY()*-1);
    			
    			if(xcord.size()>2)
    			invalidate();
    		return true;
    	}
		 
		 else
		 {
			 xcord.add((int)event.getX());
			 ycord.add((int)event.getY());
			
			 try {
				Thread.currentThread().sleep(25);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				if(xcord.size()>2)
	    			invalidate();
		 }
		 
		return true;
    }

	
	
	
	
	
}
